<?php

declare(strict_types=1);

namespace App\Controller\store;

use App\Constants\Business;
use App\Middleware\User\StoreMiddleware;
use App\Request\Store\Seller\DetailsRequest;
use App\Service\Store\BusinessService;
use App\Service\Store\SellerService;
use App\Service\Store\StoreService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 后台商家管理（非审核）
 * @AutoController()
 * @Middleware(StoreMiddleware::class)
 * Class SellerController
 * @author 陈奕文
 * @package App\Controller\store
 */
class SellerController extends Controller
{
    /**
     * 后台用户服务
     * @Inject()
     * @var StoreService
     */
    private $store_service;

    /**
     * @Inject()
     * @var BusinessService
     */
    private $business_service;

    /**
     * @Inject()
     * @var SellerService
     */
    private $seller_service;
    /**
     * 商家搜索
     */
    public function search()
    {
        $params = $this->request->all();
        // 获取所有符合条件的商家
        $business_list = $this->seller_service->getBusinessList($params);

        $this->success($business_list);
    }

    /**
     * 商家入驻方式返回
     * @return array
     */
    public function entries()
    {
        return Business::ENTRY_WAY;
    }

    /**
     * 商家详情
     * @param DetailsRequest $request
     */
    public function details(DetailsRequest $request)
    {
        $id = (int)$request->input('id');

        $result = $this->business_service->storeDetails( $id );

        $this->success($result);
    }

    /**
     * 禁用该商家
     */
    public function forbidden()
    {
        $id = (int)$this->request->input("id");
        !$id && $this->error();

        $result = $this->business_service->dao->updateByIds([
            'id' => $id,
        ], ['status' => 0]);
        !$result && $this->error("禁用失败");
        $this->success($result);
    }

}
